from util.ObjectMap import *
from util.ParseConfiurationFile import ParseConfigFile

class LoginPage:
    def __init__(self,driver):
        self.driver = driver
        self.parseCF = ParseConfigFile()
        self.loginOption = self.parseCF.getItemSection("126mail_login")
        print(self.loginOption)

    def switchToFrame(self):
        try:
            locateType, locatorExpression = self.loginOption["loginPage.frame".lower()].split(">")
            tag = getElement(self.driver,locateType,locatorExpression)
            tag.click()
            self.driver.switch_to.frame(0)
        except Exception as e:
            raise e

    def switchToDefaultFrame(self):
        try:
            self.driver.switch_to.default_content()
        except Exception as e:
            raise e

    def userNameObj(self):
        try:
            locateType, locatorExpression = self.loginOption["loginPage.username".lower()].split(">")
            elementObj = getElement(self.driver,locateType,locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def passwordObj(self):
        try:
            locateType, locatorExpression = self.loginOption["loginPage.password".lower()].split(">")
            elementObj = getElement(self.driver,locateType,locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def loginButton(self):
        try:
            locateType, locatorExpression = self.loginOption["loginPage.loginbutton".lower()].split(">")
            elementObj = getElement(self.driver,locateType,locatorExpression)
            return elementObj
        except Exception as e:
            raise e

if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get("http://mail.126.com/")
    import time
    time.sleep(1)
    login = LoginPage(driver)
    login.switchToFrame()
    login.userNameObj().send_keys('wyu0430')
    login.passwordObj().send_keys("19870303")
    login.loginButton().click()
    time.sleep(2)
    assert "草稿箱" in driver.page_source
    driver.quit()